/// @file unrootedtree-builder.cc
/// @brief Methods associated with the unrooted tree structures

// *************************************************************************

// Copyright© 2010 Texas A&M University, 
//                 College Station, Texas
//                 Contact: R Crosby <rwc@cs.tamu.edu>
//
// Copyright© 2003, 2004 by BiRC -- Bioinformatics Research Center
//                 University of Aarhus, Denmark
//                 Contact: Thomas Mailund <mailund@birc.dk>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

// *************************************************************************

#include <string>
#include <cassert>

using namespace std;

#include "unrootedtree-builder.hh"
#include "S.hh"

void
UnrootedTreeBuilder::push_internal()
{
    UnrootedTreeInner *uti = new UnrootedTreeInner();
    
    if (_stack.size() > 0)
	_stack.top()->connect(uti);
    
    _stack.push(uti);
}

void
UnrootedTreeBuilder::pop_internal()
{
    UnrootedTreeNode *n = _stack.top();
    _stack.pop();

// /    if (n->degree() == 2)
// 	{
// 	    //FIXME isnt this an error
// 	    std::cerr << "WARNING: Node with degree 2 was converted into "
// 		      << "an edge." << std::endl;
// 	    Node *c1 = n->edges()[0];
// 	    Node *c2 = n->edges()[1];

// 	    c1->replace_edge(n,c2);
// 	    c2->replace_edge(n,c1);
// 	    delete n;
// 	}
//     else
// 	{
    _completed.push_back(n);
// 	}
}

void
UnrootedTreeBuilder::add_leaf(const char *s)
{
    UnrootedTreeLeaf *utl = new UnrootedTreeLeaf(smap.Create(s));
    _completed.push_back(utl);
    
    if (_stack.size() > 0) 
	_stack.top()->connect(utl);

}

UnrootedTree *
UnrootedTreeBuilder::build_tree ()
{
    assert(build_completed());
    
    return new UnrootedTree( _completed.front() );
}
